var M_Recurl = "http://192.168.1.220:8009/";//录音调听地址配置

$(function () {

    var c_url = "iifoxHandler/CallDetails";
    var $table = $('#DataGrid');
    var $data = $('#audio'); //录音
    var $dialog = $('#dialogNew');

    $table.lgbTable({
        url: c_url,
        dataBinder: {
            map: {
            },
            callback: function (data) {

            }
        },
        smartTable: {
            idField: "StatisticDate",
            sortName: "StatisticDate",
            sortOrder: "desc",
            queryParams: function (params) {
                return $.extend(params, {
                    AgentNo: $("#txt_AgentNo").val(),//座席
                    BeginDate: $("#txt_operate_start").val(), //开始日期
                    EndDate: $("#txt_operate_end").val(),//结束日期
                    CallerID: $("#txt_CallerID").val(),//主叫号
                    CalleeID: $("#txt_CalleeID").val(),//被叫号
                    CallDir: $("#txt_CallDir").val(),//呼叫方向
                });
            },           //传递参数（*）
            columns: [
                {
                    title: "日期", field: "Date", sortable: true, class: "text-center"
                },
                { title: "开始时间", field: "BeginTime", sortable: true, class: "text-center" },
                { title: "结束时间", field: "EndTime", sortable: true, class: "text-center" },
                { title: "主叫号", field: "CallerID", sortable: true, class: "text-center" },
                { title: "被叫号", field: "CalleeID", sortable: true, class: "text-center" },
                { title: "状态", field: "Status", sortable: true, class: "text-center" },
                { title: "座席", field: "AgentNo", sortable: true, class: "text-center" },
                { title: "分机", field: "PhoneNo", sortable: true, class: "text-center" },
                {
                    title: "通话时长", field: "StatisticTime", sortable: true, class: "text-center", formatter: function (value, row, index) {
                        return newDate(value);
                    }
                },
                { title: "呼叫方向", field: "CallDir", sortable: true, class: "text-center" },
                { title: "随路数据", field: "PrivateData", sortable: true },
                { title: "CallID", field: "CallID", sortable: true, hidden: "true" },
            ],
            editButtons: {
                events: {
                    'click': function (e, value, row, index) {
                        $table.bootstrapTable('uncheckAll');
                        $table.bootstrapTable('check', index);
                    },
                    'click .detail': function (e, value, row, index) { //播放录音
                        var recurl = GetRecUrl(row.Date, row.CallID); //获取录音url
                        $data.attr("src", recurl);//给放音控件赋值
                        $dialog.modal('show');//打开modal

                    },
                    'click .down': function (e, value, row, index) { //下载
                        window.open(GetRecUrl(row.Date, row.CallID));
                    }
                },
                formatter: function (value, row, index) {
                    var $this = this.clone();
                    if (row.IsReset === 0) {
                        $this.find('button.reset').remove();
                    }
                    return $this.html();
                }
            },
            exportOptions: {
                fileName: "通话明细",
                ignoreColumn: [0, 7]
            }
        }
    });
    $table.bootstrapTable('hideColumn', 'CallID');//隐藏callid列

});
//关闭页面时，将调听录音控件清空，避免关闭后继续放音
$("#close").click(function () {
    $('#audio').attr("src", "");//清空录音控件url
});
function newDate(value) { //将秒数转为时分秒 格式 HH:mm:ss
    if (value == null || value == "") { //如果value 为空  返回空
        return "";
    }
    value = parseInt(value); //去掉小数点后数据
    var data;
    if (value > -1) {
        var hour = Math.floor(value / 3600);//小时
        var min = Math.floor(value / 60) % 60;//分钟
        var sec = value % 60;
        if (hour < 10) {
            data = '0' + hour + ":";
        } else {
            data = hour + ":";
        }
        if (min < 10) { data += "0"; }
        data += min + ":";
        if (sec < 10) { data += "0"; }
        data += sec.toFixed(0);
    }
    return data;
}

//拼接录音URL
function GetRecUrl(date, callid) {
    var RecUrl = "";
    var year = (date).substr(0, 4);//年
    var month = (date).substr(5, 2);//月
    month = ConvertMonth(month);//将月份改为英文
    var day = (date).substr(8, 2);//日
    RecUrl = M_Recurl + year + "/" + month + "/" + day + "/" + callid + ".wav";
    return RecUrl;
}

//月份转换
function ConvertMonth(iMonth) {
    var month = "";
    switch (iMonth) {
        case "01":
            month = "Jan";
            break;
        case "02":
            month = "Feb";
            break;
        case "03":
            month = "Mar";
            break;
        case "04":
            month = "Apr";
            break;
        case "05":
            month = "May";
            break;
        case "06":
            month = "Jun";
            break;
        case "07":
            month = "Jul";
            break;
        case "08":
            month = "Aug";
            break;
        case "09":
            month = "Sep";
            break;
        case "10":
            month = "Oct";
            break;
        case "11":
            month = "Nov";
            break;
        case "12":
            month = "Dec";
            break;
        default:
            break;
    }

    return month;
}